#!/bin/bash

# Remote Build Script for YOLOv8 OpenVINO Project
# Usage: ./remote-build.sh <host> <user> <password> [build-type]
# Example: ./remote-build.sh 192.168.10.11 asr 123456 full

set -e

# Configuration
REMOTE_HOST="${1:-192.168.10.11}"
REMOTE_USER="${2:-asr}"
REMOTE_PASSWORD="${3:-123456}"
BUILD_TYPE="${4:-full}"  # full, cpp, go, test
REMOTE_PATH="/home/asr/git/goyolopenvino"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Helper functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

# Check if sshpass is installed
check_sshpass() {
    if ! command -v sshpass &> /dev/null; then
        log_error "sshpass is not installed"
        log_info "Install it with: brew install sshpass (macOS) or apt-get install sshpass (Linux)"
        exit 1
    fi
}

# SSH command wrapper
ssh_exec() {
    local cmd="$1"
    sshpass -p "$REMOTE_PASSWORD" ssh -o StrictHostKeyChecking=no \
        "${REMOTE_USER}@${REMOTE_HOST}" "$cmd"
}

# SCP wrapper
scp_to_remote() {
    local local_file="$1"
    local remote_path="$2"
    sshpass -p "$REMOTE_PASSWORD" scp -o StrictHostKeyChecking=no \
        "$local_file" "${REMOTE_USER}@${REMOTE_HOST}:${remote_path}"
}

# Main build functions
build_cpp() {
    log_info "Building C++ component on remote host..."
    
    ssh_exec "cd ${REMOTE_PATH}/YOLOv8-OpenVINO-CPP-Inference && \
        echo '=== Checking dependencies ===' && \
        which cmake || echo 'cmake not found' && \
        which g++ || echo 'g++ not found' && \
        pkg-config --modversion opencv4 || pkg-config --modversion opencv || echo 'OpenCV not found' && \
        echo '=== Starting build ===' && \
        make clean && \
        make build 2>&1"
    
    if [ $? -eq 0 ]; then
        log_success "C++ build completed"
    else
        log_error "C++ build failed"
        return 1
    fi
}

build_go() {
    log_info "Building Go component on remote host..."
    
    ssh_exec "cd ${REMOTE_PATH}/goyolo && \
        echo '=== Checking Go version ===' && \
        go version && \
        echo '=== Downloading dependencies ===' && \
        go mod download && \
        go mod tidy && \
        echo '=== Starting build ===' && \
        make clean && \
        make build 2>&1"
    
    if [ $? -eq 0 ]; then
        log_success "Go build completed"
    else
        log_error "Go build failed"
        return 1
    fi
}

run_tests() {
    log_info "Running tests on remote host..."
    
    ssh_exec "cd ${REMOTE_PATH}/goyolo && \
        echo '=== Running Go tests ===' && \
        go test -v ./... 2>&1"
    
    if [ $? -eq 0 ]; then
        log_success "Tests completed"
    else
        log_warning "Some tests failed"
    fi
}

check_environment() {
    log_info "Checking remote environment..."
    
    ssh_exec "echo '=== System Info ===' && \
        uname -a && \
        echo '' && \
        echo '=== Go Version ===' && \
        go version 2>/dev/null || echo 'Go not installed' && \
        echo '' && \
        echo '=== CMake Version ===' && \
        cmake --version 2>/dev/null || echo 'CMake not installed' && \
        echo '' && \
        echo '=== GCC Version ===' && \
        g++ --version 2>/dev/null || echo 'GCC not installed' && \
        echo '' && \
        echo '=== OpenCV ===' && \
        pkg-config --modversion opencv4 2>/dev/null || pkg-config --modversion opencv 2>/dev/null || echo 'OpenCV not installed' && \
        echo '' && \
        echo '=== OpenVINO ===' && \
        ls -la /opt/intel/openvino 2>/dev/null || echo 'OpenVINO not found in /opt/intel/openvino' && \
        echo '' && \
        echo '=== Project Directory ===' && \
        ls -la ${REMOTE_PATH} 2>/dev/null || echo 'Project directory not found'"
}

# Main execution
main() {
    log_info "Remote Build Script Started"
    log_info "Target: ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"
    log_info "Build Type: ${BUILD_TYPE}"
    echo ""
    
    # Check prerequisites
    check_sshpass
    
    # Test SSH connection
    log_info "Testing SSH connection..."
    if ssh_exec "echo 'SSH connection successful'" > /dev/null 2>&1; then
        log_success "SSH connection established"
    else
        log_error "Failed to connect to remote host"
        exit 1
    fi
    echo ""
    
    # Check environment
    check_environment
    echo ""
    
    # Execute build based on type
    case "$BUILD_TYPE" in
        full)
            build_cpp && build_go && run_tests
            ;;
        cpp)
            build_cpp
            ;;
        go)
            build_go
            ;;
        test)
            run_tests
            ;;
        *)
            log_error "Unknown build type: $BUILD_TYPE"
            log_info "Valid types: full, cpp, go, test"
            exit 1
            ;;
    esac
    
    echo ""
    log_success "Build process completed!"
}

# Run main function
main "$@"

